<?php
/**
 * Created by PhpStorm.
 * User: jieai
 * Date: 2017-04-04
 * Time: 21:04
 */
namespace Home\Controller;

use Think\Controller;

class RecordController extends MyController
{
    public function Index()
    {
        $members = M('member')->field('id,name')->select();
        $this->assign('members', $members);
        $stations=M('station')->field('id,name')->select();
        $this->assign('stations',$stations);
        $this->display();
    }

    /**
     *
     */
    public function getrecords()
    {


        //region 根据传过来的开始时间，结束时间是否审核来生成过滤条件
        $starttime = I('starttime');
        $donetime = I('donetime');
        $pendid = I('pendid', -1);
        $map = array();
        if ($starttime) {
            $starttime = strtotime($starttime);
            $map['starttime'][] = array('egt', $starttime);
        }
        if ($donetime) {
            $donetime = strtotime($donetime);
            $map['starttime'][] = array('elt', $donetime);
        }
        $map['pend'] = array('neq', $pendid);
        //endregion


        //region 可以根据传过来的memberid来查找gsrecord的id
        $memids = I('memid', array(0));
        $membermap = array();
        if (!in_array(0, $memids)) {
            $membermap['member_id'] = array('in', $memids);
            $rowresult = M('member_gsrecord')->where($membermap)->distinct(true)->getField('gsrecord_id', true);

            $map['id'] = array('in', $rowresult);
        }
        //
        $stationid=I('stationsid',array(0));
        if (!in_array(0,$stationid)) {
            $map['station_id']=array('in',$stationid);
        }




        $page = I('page', 1);
        $rows = I('rows', 10);
        $offset = ($page - 1) * $rows;
        $map['status']=0;
        $rowresult = D('RecordRelation')->relation(true)->where($map)->order('starttime desc')->limit($offset, $rows)->select();
        $total = M('gsrecord')->where($map)->count();
        $result = array();
        $result['total'] = $total;
        $result['rows'] = $rowresult;



        return $this->ajaxReturn($result);
    }

    public function deleterecord()
    {
        $this->Checklogin();
        $id=I('id');
        if($id){
            $recordR=M('gsrecord');
            $recordR->id=$id;
            $recordR->status=1;
            $result= $recordR->save();
//            $recordR = D('RecordRelation');
//            $result=$recordR->relation('member')->delete($id);
            if ($result) {
                $this->success('删除成功');
            }
        }
        $this->error('什么也没做!');


        //todo  应该在编辑界面添加审核的按钮,可以用switchbutton做
        //todo  我决定把工时记录做成软删除,把remarks改成了status.为1是为删除.还得把所有查找改了
    }

    public function test()
    {
        $result = D('RecordRelation');

        $data['station_id'] = 100;
        $data['id'] = 4;
        $mem = M('member')->select();


        $data['member'] = $mem;

        // $result->relation(true)->save($data);

        $result = D('RecordRelation')->relation(true)->select();
        P($result);
    }

    public function add()
    {
        $memlist = M('member')->select();
        $stationlist = M('station')->select();
        $this->assign('memlist', $memlist);
        $this->assign('stationlist', $stationlist);

        $this->display();
    }



    public function add_post()
    {
        $pend=I('pend');
       $pend=$pend?1:0;
        
        
        $temp = I('starttime');

//        $ttt=  new \Org\Util\Date();
//        $sss=$ttt->parse($temp);//这是thinkphp带的时间函数
        $ss = strtotime($temp);//这个函数方便的转换时间

//        P($_POST);
//        die;
        if (IS_POST) {
            $record = D('recordRelation');
            $record->station_id = I('station');
            $record->content = I('content', '', '');
            $record->pend=$pend;
            $record->starttime = strtotime(I('starttime'));
            $record->donetime = strtotime(I('donetime'));
            $record->createtime = time();
            $record->createip = get_client_ip();
            $rid = I('id');//如果参数中有id参数.表示为修改,否则为添加
            $result = null;
            if (!$rid) {
                $result = $record->add();
            } else {
                $record->id = $rid;
                $result = $record->save();
            }
            if ($result) {
                if ($rid) {//如果有结果并且有id，说明是编辑。需要先把原始的member_gsvalue数据删掉
                    $members_gs = M('member_gsrecord');
                    $members_gs->where("gsrecord_id=$rid")->delete();
                }
                $members = I('member');
                $membervalues = I('member_value');
	            $membervalues = array_filter($membervalues);
	            $membervalues = array_values($membervalues);
                foreach ($members as $itemid => $item) {
                    $temp = M('member_gsrecord');
                    $temp->member_id = $item;
                    $temp->gsrecord_id = $rid ? $rid : $result;
	                $temp->gsvalue = $membervalues[$itemid] ? $membervalues[$itemid] : 0;
                    $temp->add();
                }
                $this->MyAutoCloseSuccess('操作成功!');


            } else {
                $this->MyAutoCloseError('操作失败!');
            }

        }
    }

    public function edit()
    {
//
        $this->Checklogin();
        $record = D('recordRelation')->relation(true)->find(I('id'));


        //region 获得选择的memid
        $condition['gsrecord_id'] = $record['id'];
        $memids = M('Member_gsrecord')->where($condition)->getField('member_id,gsvalue', true);//这个方法注意一下.必须传true
        $this->assign('memids', json_encode($memids));
        //endregion

        //<editor-fold desc="传递选择的stationid">
        $this->assign('stationid', $record['station_id']);
        //</editor-fold>


        //region 传递外键信息
        $memlist = M('member')->select();
        $this->assign('memlist', $memlist);
        $stationlist = M('station')->select();
        $this->assign('stationlist', $stationlist);
        //endregion


        $stime = date('Y-m-d H:i', $record['starttime']);
        $dtime = date('Y-m-d H:i', $record['donetime']);
        $this->assign('stime', $stime);
        $this->assign('dtime', $dtime);

        $this->assign('content', htmlspecialchars_decode($record['content']));

        $this->assign('record', $record);
        $this->display();
    }

    public function pendrecord()
    {
        $this->Checklogin();
        if (IS_AJAX) {
            $rid = I('id');
            $record = M('gsrecord');
            $record->id = $rid;
            $record->pend = 1;
            $result = $record->save();
            if ($result) {
                $this->success('审核成功');
            }
            $this->error('审核失败!');

        }
    }
}
